//
//  BrowseImageViewController.swift
//  ezyform
//
//  Created by MoennigQ on 5/3/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
struct BrowseImageData {
    var image: String = ""
    var code: String = ""
    var codeType: CodeType = .None
}
enum CodeType: String {
    case Inverter = "Inverter"
    case Panel = "Panel"
    case None = ""
}
class BrowseImageViewController: BaseViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {
    
    let navigationBar = NavigationBarView()
    var imagesArray: [BrowseImageData] = []
    var currentIndex: Int = 0
    var controlls: [PVDPhotoViewController] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .black
        //
        navigationBar.setTitle("\(currentIndex + 1)/\(imagesArray.count)")
        navigationBar.backToPreBtn.addTarget(self, action: #selector(backToPre), for: UIControl.Event.touchUpInside)
        self.view.addSubview(navigationBar)
        navigationBar.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            
        }
        // 1> 创建一个 PageViewController
        /// transitionStyle（UIPageViewControllerTransitionStyle）: 展示的效果是卷轴效果还是滚动效果
        /// navigationOrientation（UIPageViewControllerNavigationOrientation） : 滚动的方向
        /// UIPageViewControllerOptionInterPageSpacingKey  : 水平的间距
        let pageVC = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [UIPageViewController.OptionsKey.interPageSpacing : 5])
        
        // 2> 将 pageVC 添加到父控制器中
        self.addChild(pageVC)
        // 3> 将 pageVC 的View 添加到父控制器的View上
        self.view.addSubview(pageVC.view)
        pageVC.view.snp.makeConstraints { make in
            make.top.equalTo(navigationBar.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        pageVC.didMove(toParent: self)
        
        for i  in 0 ..< self.imagesArray.count {
            let vc = PVDPhotoViewController(self.imagesArray[i])
            vc.refreshTitleClosure = {[weak self] in
                guard let strongSelf = self else { return  }
                strongSelf.navigationBar.setTitle("\(i + 1)/\(strongSelf.imagesArray.count)")
            }
            controlls.append(vc)
        }
        // 4> 添加子控制器
//        let photoVC = PVDPhotoViewController(index: currentIndex, imagesArray: imagesArray)
        pageVC.setViewControllers([controlls[currentIndex]], direction: .forward, animated: true, completion: nil);
        
        // 5> 设置数据源
        pageVC.dataSource = self
        pageVC.delegate = self
    }
    
    /**
     返回当前页面的前一个控制器
     
     - parameter pageViewController: 当前的  pageViewController
     - parameter viewController:     当前的子控制器
     ddd
     - returns: 前一个子控制器
     */
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        //只有一张图片就不处理翻页
        if self.controlls.count == 1 {   return nil }
        var index = self.controlls.firstIndex(of: viewController as! PVDPhotoViewController) ?? 0
        if (index == 0) {
            index = self.controlls.count - 1;
        } else {
            index = index - 1
        }
        return self.controlls[index]
    }
    
    /**
     返回当前页面的后一个控制器
     
     - parameter pageViewController: 当前的  pageViewController
     - parameter viewController:     当前的子控制器
     
     - returns: 后一个子控制器
     */
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        //只有一张图片就不处理翻页
        if self.controlls.count == 1 {   return nil }
        var index = self.controlls.firstIndex(of: viewController as! PVDPhotoViewController) ?? 0
        if (index == self.controlls.count - 1) {
            index = 0;
        } else {
            index = index + 1
        }
        return self.controlls[index]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            _ = pageViewController.viewControllers?.first as? PVDPhotoViewController
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
}
